package com.cybertron.ironhide.search.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cybertron.ironhide.search.common.base.Pager;
import com.cybertron.ironhide.search.service.ProductService;
import com.cybertron.ironhide.search.util.FiltersUtil;

@Controller
public class SearchController {

	@Autowired(required = false)
	private ProductService prodcutService;

	@RequestMapping(value = "/wd--{keyWord}", method = RequestMethod.GET)
	public String search(
			@PathVariable("keyWord") String keyWord,
			@CookieValue(value = "viewType", required = false, defaultValue = "list") String viewType,
			Pager pager, Model model) {
		Map<String, Object> result = prodcutService
				.search(keyWord, null, pager);
		model.addAllAttributes(result);
		model.addAttribute("pager", pager);
		return toView(null, viewType, model);
	}

	@RequestMapping(value = "/cate--{categoryId}", method = RequestMethod.GET)
	public String searchCate(
			@PathVariable("categoryId") String categoryId,
			@CookieValue(value = "viewType", required = false, defaultValue = "list") String viewType,
			Pager pager, Model model) {
		Map<String, String> filters = new LinkedHashMap<String, String>();
		filters.put("filterCategoryId", categoryId);
		Map<String, Object> result = prodcutService
				.search(null, filters, pager);
		model.addAllAttributes(result);
		model.addAttribute("pager", pager);
		model.addAllAttributes(filters);
		return toView(filters, viewType, model);
	}

	@RequestMapping(value = "/cate--{categoryId}/wd--{keyWord}", method = RequestMethod.GET)
	public String searchCate(
			@PathVariable("categoryId") String categoryId,
			@PathVariable("keyWord") String keyWord,
			@CookieValue(value = "viewType", required = false, defaultValue = "list") String viewType,
			Pager pager, Model model) {
		Map<String, String> filters = new LinkedHashMap<String, String>();
		filters.put("filterCategoryId", categoryId);
		Map<String, Object> result = prodcutService.search(keyWord, filters,
				pager);
		model.addAllAttributes(result);
		model.addAttribute("pager", pager);
		model.addAllAttributes(filters);
		return toView(filters, viewType, model);
	}

	@RequestMapping(value = "/cate--{categoryId}/{filterStr}/go", method = RequestMethod.GET)
	public String searchCateFilters(
			@PathVariable("categoryId") String categoryId,
			@PathVariable("filterStr") String filterStr,
			@CookieValue(value = "viewType", required = false, defaultValue = "list") String viewType,
			Pager pager, Model model) {
		Map<String, String> filters = FiltersUtil.filter(filterStr);
		filters.put("filterCategoryId", categoryId);
		Map<String, Object> result = prodcutService
				.search(null, filters, pager);
		model.addAllAttributes(result);
		model.addAttribute("pager", pager);
		model.addAttribute("viewType", viewType);
		model.addAllAttributes(filters);
		return toView(filters, viewType, model);
	}

	@RequestMapping(value = "/wd--{keyWord}/{filterStr}/go", method = RequestMethod.GET)
	public String searchFilters(
			@PathVariable("keyWord") String keyWord,
			@PathVariable("filterStr") String filterStr,
			@CookieValue(value = "viewType", required = false, defaultValue = "list") String viewType,
			Pager pager, Model model) {
		Map<String, String> filters = FiltersUtil.filter(filterStr);
		Map<String, Object> result = prodcutService.search(keyWord, filters,
				pager);
		model.addAllAttributes(result);
		model.addAttribute("pager", pager);
		model.addAttribute("viewType", viewType);
		model.addAllAttributes(filters);
		return toView(filters, viewType, model);
	}

	@RequestMapping(value = "/cate--{categoryId}/wd--{keyWord}/{filterStr}/go", method = RequestMethod.GET)
	public String searchFilters(
			@PathVariable("categoryId") String categoryId,
			@PathVariable("keyWord") String keyWord,
			@PathVariable("filterStr") String filterStr,
			@CookieValue(value = "viewType", required = false, defaultValue = "list") String viewType,
			Pager pager, Model model) {
		Map<String, String> filters = new LinkedHashMap<String, String>();
		filters.put("filterCategoryId", categoryId);
		FiltersUtil.filter(filters, filterStr);
		Map<String, Object> result = prodcutService.search(keyWord, filters,
				pager);
		model.addAllAttributes(result);
		model.addAttribute("pager", pager);
		model.addAllAttributes(filters);
		return toView(filters, viewType, model);
	}

	private String toView(Map<String, String> filters, String viewType,
			Model model) {
		if (filters != null && filters.get("filterView") != null) {
			if (StringUtils.equals(filters.get("filterView"), "grid")) {
				model.addAttribute("viewType", "grid");
				return "search_grid";
			} else if (StringUtils.equals(filters.get("filterView"), "list")) {
				model.addAttribute("viewType", "list");
				return "search_list";
			}
		} else if ("grid".equals(viewType)) {
			model.addAttribute("viewType", "grid");
			return "search_grid";
		}
		model.addAttribute("viewType", "list");
		return "search_list";
	}
}
